package com.zby.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zby.lease.model.enums.LeaseStatus;
import com.zby.lease.model.pojo.LeaseAgreement;
import com.zby.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
// 定时任务
@Component
public class ScheduledTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    // 每天午夜执行检查租赁协议状态的任务
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseAgreementStatus() {
        // 创建Lambda更新封装器，用于构建更新条件
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        // 获取当前日期时间
        Date date = new Date();
        // 设置更新条件：租赁结束日期小于等于当前日期
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, date);
        // 设置更新条件：协议状态在已签订或撤回中
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        // 设置更新内容：协议状态为已到期
        updateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        // 执行更新操作，以更改符合上述条件的协议状态
        leaseAgreementService.update(updateWrapper);
    }

    }

